package _20动态代理案例;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class Demo20 {
    public static void main(String[] args) throws IOException {
        List<String> list = new ArrayList<>();

        List proxyInstance = (List) Proxy.newProxyInstance(Demo20.class.getClassLoader(), new Class[]{List.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("remove") ) {
                    System.out.println("remove方法被禁用");
                    return Boolean.valueOf(false);

                }else if (method.getName().equals("addAll")){
                    FileWriter fileWriter = new FileWriter("day13\\src\\test\\20.text",true);
                    fileWriter.write((String) args[0]);
                    fileWriter.write("\r\n");
                    fileWriter.close();

                }
                    Object invoke = method.invoke(list, args);
                    return invoke;



            }
        });
        proxyInstance.add("你好");
        proxyInstance.add("你好吗");
//        proxyInstance.addAll("一","二");
        proxyInstance.remove(1);
        System.out.println(proxyInstance);
    }
}
